package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.service.SpecificationService;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("Specification")
public class SpecificationController {

    @Reference(timeout = 5000)
    private SpecificationService specificationService;

    @RequestMapping("list")
    public String list(){
        return "admin/specification";
    }


    @RequestMapping("select")
    @ResponseBody
    public Result select(String specName){
        List<TbSpecification> tbSpecifications = specificationService.select(specName);
        if(tbSpecifications == null || tbSpecifications.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbSpecifications);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(String optionlistjson,String name){
        List<TbSpecificationOption> list = JSONObject.parseArray(optionlistjson, TbSpecificationOption.class);
        boolean b = specificationService.insertAll(list,name);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }else {
            return Result.builResult(ResultEnum.ERROR);
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long id){
        boolean b = specificationService.delete(id);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }else {
            return Result.builResult(ResultEnum.ERROR);
        }
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id){
        try {
            Map<String,Object> map = new LinkedHashMap<>();
//        查询规格表
            TbSpecification specification = specificationService.toupdate(id);
            map.put("specification",specification);
//        查询中间表
            List<TbSpecificationOption> specificationOption = specificationService.toupte(id);
            map.put("specificationOption",specificationOption);
            return Result.builResult(ResultEnum.SUCCESS).setResout(map);
        }catch (Exception e){
            return Result.builResult(ResultEnum.ERROR);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(String optionlistjson,TbSpecification tbSpecification){
        List<TbSpecificationOption> list = JSONObject.parseArray(optionlistjson, TbSpecificationOption.class);
        boolean b = specificationService.update(list,tbSpecification);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }else {
            return Result.builResult(ResultEnum.ERROR);
        }
    }
}
